﻿using System;
using System.Collections;
using System.Configuration;
using System.Data.Odbc;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;
public partial class ChangePassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("LogoutButton").Visible = false;
        if ((String)Session["Username"] == null || (String)Session["Username"] != "admin")
        {
            //Response.End();
            Response.Redirect("~/LoginAdmin.aspx");
        }
    }

    static string getMd5Hash(string input)
    {
        // Create a new instance of the MD5CryptoServiceProvider object.
        MD5 md5Hasher = MD5.Create();

        // Convert the input string to a byte array and compute the hash.
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data 
        // and format each one as a hexadecimal string.
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string.
        return sBuilder.ToString();
    }


    protected void LoginView1_ViewChanged(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DbUtilities db = new DbUtilities();
        string oldPassword = getMd5Hash(TextBox1.Text);
        string newPassword = getMd5Hash(TextBox2.Text);
        string confirmPassword = getMd5Hash(TextBox3.Text);
        if (oldPassword.Length != 0)
        {
            if (newPassword == confirmPassword)
            { // Response.Write("You are here ");
                bool isConfirm = true;
                isConfirm = db.checkDatabaseForOldPassword(oldPassword);
                if (isConfirm == true)
                {
                    db.changePassword(oldPassword, newPassword);
                    Response.Redirect("~/LoginAdmin.aspx");
                }

                else Response.Write("Sorry , but you entered Wrong password");
            }
            else Response.Write("Ensure that you entered the same password in the Confirm password");
        }
        else Response.Write("Dont leave the field empty ");
    }

}




